package xin.printk.android.module.register;

import xin.printk.android.BasicApplication;
import xin.printk.android.R;
import xin.printk.android.utils.RequestCallback;

public class RegisterPresenter implements RegisterContract.Presenter {

    private RegisterContract.View mRegisterView;
    private RegisterModel mRegisterModel;

    public RegisterPresenter(RegisterContract.View view) {
        mRegisterView = view;
        mRegisterModel = new RegisterModel();
    }

    @Override
    public void getVerifyCode() {
        mRegisterView.mockVerifyCode();
    }

    @Override
    public void register() {
        if (mRegisterView.getAccount().isEmpty()) {
            mRegisterView.showMessage(BasicApplication.getContext().getString(R.string.account_null_tips));
            return;
        }

        if (mRegisterView.getPassword().isEmpty()) {
            mRegisterView.showMessage(BasicApplication.getContext().getString(R.string.password_null_tips));
            return;
        }

        if (!mRegisterView.isVerifyCodeValid()) {
            mRegisterView.showMessage(BasicApplication.getContext().getString(R.string.verity_code_error_tips));
            return;
        }

        mRegisterModel.register(mRegisterView.getAccount(), mRegisterView.getPassword(), new RequestCallback() {
            @Override
            public <T> void onSuccess(T t) {
                mRegisterView.registerSuccess();
            }

            @Override
            public void onFailure(String message) {
                mRegisterView.showMessage(message);
            }
        });
    }
}
